<?php
class Entities implements Iterator
{
	private
		$entities = array();
	
	// Basic methods //
	protected function add($key, Entity $Entity)
	{
		$this->entities[$Entity->$key] = $Entity; return $this;
	}
	
	public function find($key)
	{
		return isset($this->entities[$key])?$this->entities[$key]:null;
	}
	// array methods //
	public function pull() { return array_pop($this->entities); }
	
	public function toArray() { return $this->entities; }
	
	// Cool stuffs //
	public function join($separator, $pattern)
	{
		$str = null;
		foreach($this->entities as $Entity)
		{
			$str .= ($str?$separator:'') . $Entity->map($pattern);
		}
		return $str;
	}
	
	public function map($pattern)
	{
		$arr = array();
		foreach($this->entities as $Entity)
		{
			$arr[] = $Entity->map($pattern);
		}
		return $arr;
	}	
	
	// implements Iterator //
	public function rewind() { reset($this->entities); }
	public function key() { return key($this->entities); }
	public function current() { return current($this->entities); }
	public function next() { return next($this->entities); }
	public function valid() { return current($this->entities); }
}